from django.db import models
from django.contrib.auth.models import User


# Create your models here.
def toSafeURL(plain_text):
    return plain_text.replace(' ', "%20")

def fromSafeURL(safe_url):
    return safe_url.replace("%20", ' ')
    
                   
class Area(models.Model):
    user = models.ForeignKey(User)
    area_name = models.CharField(max_length=40)
    house_code = models.CharField(max_length=1)

    def __unicode__(self):
        return self.area_name
    
    def toURL(self):
        return '<a href="/jarvis/areas/%s">%s</a>'%(toSafeURL(self.area_name), self.area_name)    
    
class Device(models.Model):
    user = models.ForeignKey(User)
    device_name = models.CharField(max_length=40)
    device_code = models.IntegerField()
    area = models.ForeignKey(Area)
    active = models.BooleanField()
    enabled = models.BooleanField()

    def __unicode__(self):
        return self.device_name

    def __full__(self):
        return self.device_name + " " + str(user) + ' ' + device_code + ' ' + str(area) + ' ' +  str(active) + " " + str(enabled)

    def toURL(self):
        return '<a href="/jarvis/areas/%s/%s">%s</a>'%(toSafeURL(self.area.area_name), toSafeURL(self.device_name), self.device_name)

class Group(models.Model):
    user = models.ForeignKey(User)
    group_name = models.CharField(max_length=30)
    device_list = models.CharField(max_length=30)
    active = models.BooleanField()
    enabled = models.BooleanField()

    def __unicode__(self):
        return self.group_name
